package com.up.vms.infrastructure.job;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

/**
 * Created by wangshd on 2018/12/5.
 */
@Component
public class JobManager {

    @Autowired
    JobConfiguration jobConfiguration;

    public boolean addJob(JobParameter jobParameter)throws Exception{
        ResponseEntity<String> responseEntity = HttpClientUtil.post(jobConfiguration.getServerURL()+"add", jobParameter, "form");
        return this.handleResponse(responseEntity);

    }

    public boolean removeJob(JobParameter jobParameter)throws Exception{
        ResponseEntity<String> responseEntity = HttpClientUtil.post(jobConfiguration.getServerURL()+"remove", jobParameter, "form");
        return this.handleResponse(responseEntity);
    }

    private boolean handleResponse(ResponseEntity<String> responseEntity){
        boolean isSucceed = false;
        if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
            String respData = responseEntity.getBody();
            JobResponse jobResponse = JSON.parseObject(respData, JobResponse.class);
            if(jobResponse.getCode().equals("200")){
                isSucceed = true;
            }
        }
        return isSucceed;
    }

}
